package com.lkm.app_ui.ui.sale;

import android.graphics.Bitmap;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.QRCodeUtil;
import com.lkm.comlib.Config;
import com.lkm.comlib.help.ViewHelp;
import com.lkm.frame.task2.CTaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskCollection;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.entity.FairTo;
import com.lkm.market2_0.entity.PayTo;

import java.util.ArrayList;



public class QrUIView {


    private TaskCollection taskCollection;
    private int layoutType = 0;

    private boolean canShow() {
        return layoutType == R.layout.fragment_sale1;
    }

    private View view;


    

    private String gpcQr = "";
    private String gpcTips = "";

    private ViewGroup contentGroup;
    public void showQr(int layoutType, ViewGroup viewGroup) {
        this.layoutType = layoutType;
        if (!canShow()) return;
        if (taskCollection != null)
            taskCollection.cancelAllTask();
        taskCollection = new TaskCollection(AppConst.getTaskManager2());

        contentGroup = ((ViewGroup) viewGroup.findViewById(R.id.vg_qr));
        contentGroup.removeAllViews();

        FairTo fair = RunTimeData.steelyardInfo == null ?null: RunTimeData.steelyardInfo.getFair();
        if (fair != null && !StringHelp.isEmpty(fair.url)) {
            gpcQr = fair.url;
            gpcTips = fair.tips;
        }else {
            gpcQr = "";
        }

        contentGroup.removeAllViews();
        showPayItem();
        showGPC();

    }

    private boolean isGPC() {

        return !StringHelp.isEmpty(gpcQr);
    }
    private void showGPC(){
//        findViewById(R.id.v_gpc).setVisibility(View.GONE);
        if(!isGPC())return;

        show(R.layout.sale1_qr_gpc);

        ViewGroup viewGroup = findViewById(R.id.v_gpc);
        viewGroup.setVisibility(View.VISIBLE);
        TextView tv_title = viewGroup.findViewWithTag("tv_title");
        ImageView img_qr = viewGroup.findViewWithTag("img_qr");

        tv_title.setText(gpcTips);

        setImage(img_qr, gpcQr);

    }

    private void show(int layoutid){
        contentGroup.removeAllViews();
        view = ViewHelp.inflate(contentGroup.getContext(), layoutid, contentGroup, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


    }

    private void showPayItem() {
        if (isGPC()) return;

        
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems = new ArrayList<>();
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.add(new PayTo.PayOutlineItemTo());
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.get(0).title = "123";
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.get(0).url = "123";
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.add(new PayTo.PayOutlineItemTo());
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.get(1).title = "1234";
//        RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.get(1).url = "1235";
////        RunTimeData.steelyardInfo.getPayInfo().pay_style_show = 0;
//        RunTimeData.steelyardInfo.getPayInfo().pay_style_show = 1;


        if (CollectionHelp.isEmpty(RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems))return;

        PayTo payTo = RunTimeData.steelyardInfo.getPayInfo();
        if(payTo!=null && payTo.isShowOutLinePayQrBig()){
            show(R.layout.sale1_qr_paybig);
        }else {
            show(R.layout.sale1_qr_pay);
        }


        ViewGroup v_pay_1 = findViewById(R.id.v_pay_1);
        ViewGroup v_pay_2 = findViewById(R.id.v_pay_2);
        TextView tv_tips_pay = findViewById(R.id.tv_tips_pay);

        v_pay_1.setVisibility(View.GONE);
        v_pay_2.setVisibility(View.GONE);
        if (tv_tips_pay != null)
            tv_tips_pay.setVisibility(View.GONE);

         {
            
            int size = Math.min(RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.size(), 2);

             if (size > 0 && tv_tips_pay != null) {
                tv_tips_pay.setVisibility(View.VISIBLE);
            }
            for (int i = 0; i < size; i++) {
                PayTo.PayOutlineItemTo payOutlineItem = RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.get(i);
                ViewGroup viewGroup = i == 0 ? v_pay_1 : v_pay_2;
                viewGroup.setVisibility(View.VISIBLE);
                ImageView img_icon = viewGroup.findViewWithTag("img_icon");
                TextView tv_title = viewGroup.findViewWithTag("tv_title");
                ImageView img_qr = viewGroup.findViewWithTag("img_qr");


                tv_title.setVisibility(View.VISIBLE);
                tv_title.setText(payOutlineItem.title);
                img_icon.setImageResource(R.mipmap.ic_pay_yd);
                if (payOutlineItem.title == null) {

                } else if (payOutlineItem.title.contains("银联")) {
                    img_icon.setImageResource(R.mipmap.ic_pay_yl);

                } else if (payOutlineItem.title.contains("移动")) {
                    img_icon.setImageResource(R.mipmap.ic_pay_yd);

                } else if (payOutlineItem.title.contains("微信")) {
                    img_icon.setImageResource(R.mipmap.ic_pay_wx);

                } else if (payOutlineItem.title.contains("支付宝")) {
                    img_icon.setImageResource(R.mipmap.ic_pay_ali);
                }
                setImage(img_qr, payOutlineItem.url);

            }

        }

    }

    private <T extends View> T findViewById(int id) {
        return view.findViewById(id);
    }


    public void setImage(ImageView image, String s) {
        CreateQR createQR = new CreateQR(image);
        createQR.setParam(new Object[]{s});
        createQR.action();
        taskCollection.add(createQR.execTask());
    }

    public void destroy() {
        if (taskCollection != null) taskCollection.cancelAllTask();
    }

    protected class CreateQR extends TaskAdapter<Object[], Void, Bitmap> {
        ImageView img_qr;

        public CreateQR(ImageView img_qr) {
            super(new CTaskExecutor<Object[], Void, Bitmap>(Config.getTaskExecutor(), Looper.getMainLooper()), CreateQR.class.getName(), MyApplication.getInstance().getTaskManager2());
            this.img_qr = img_qr;
        }

        @Override
        public void doExecuting(Object[] param) {
            super.doExecuting(param);
            mTaskExecutor.executorDoAsyncExecuting(new RunnablePR<Object[], Void>() {
                @Override
                public Void run(Object[] objects) {
                    complete(onExecuting(objects));
                    return null;
                }
            }, param);
        }

        public Bitmap onExecuting(Object[] param) {
            int ip = -1;
            String orderurl = (String) param[++ip];
            Bitmap bitmap = QRCodeUtil.createImage(orderurl, 400, 400);
            return bitmap;
        }

        @Override
        public void onFinish(Bitmap bitmap) {
            super.onFinish(bitmap);

            try {
                img_qr.setImageBitmap(bitmap);
            } catch (Exception e) {
            }

        }
    }

}
